package com.yuexun.beilunpatient.ui.main.ui;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.AppContext;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.base.ExitApplication;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.ui.bean.ActValue;
import com.yuexun.beilunpatient.ui.consult.ui.Frag_Consult;
import com.yuexun.beilunpatient.ui.main.bean.PushData;
import com.yuexun.beilunpatient.ui.main.fragment.Frag_Health;
import com.yuexun.beilunpatient.ui.main.fragment.Frag_Interact;
import com.yuexun.beilunpatient.ui.my.ui.Frag_My;
import com.yuexun.beilunpatient.ui.question.ui.Act_Question;

import org.kymjs.kjframe.ui.KJFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseKJActivity {
    @Bind(R.id.main_content)
    FrameLayout mainContent;
    @Bind(R.id.visit_icon)
    ImageView visitIcon;
    @Bind(R.id.visit_txt)
    TextView visitTxt;
    @Bind(R.id.visit)
    LinearLayout visit;
    @Bind(R.id.visit_circle)
    ImageView visitCircle;
    @Bind(R.id.doctor_icon)
    ImageView doctorIcon;
    @Bind(R.id.doctor_txt)
    TextView doctorTxt;
    @Bind(R.id.doctor)
    LinearLayout doctor;
    @Bind(R.id.doctor_circle)
    ImageView doctorCircle;
    @Bind(R.id.health_icon)
    ImageView healthIcon;
    @Bind(R.id.health_txt)
    TextView healthTxt;
    @Bind(R.id.health)
    LinearLayout health;
    @Bind(R.id.health_circle)
    ImageView healthCircle;
    @Bind(R.id.my_icon)
    ImageView myIcon;
    @Bind(R.id.my_txt)
    TextView myTxt;
    @Bind(R.id.my)
    LinearLayout my;
    @Bind(R.id.my_circle)
    ImageView myCircle;

    KJFragment fragVisit;
    Frag_Interact fragDoctor;
    Frag_Health fragHealth;
    Frag_My fragMy;

    ICallBackListener clickListener;

    private AppContext appContext;
    public String custom;
    private Boolean isActive = false;


    public void setClickListener(ICallBackListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ExitApplication.getInstance().addActivity(this);
    }

    @Override
    public void initData() {
        super.initData();
        initFragment();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    private void initFragment() {
        fragVisit = new Frag_Consult();
        fragDoctor = new Frag_Interact();
        fragHealth = new Frag_Health();
        fragMy = new Frag_My();

        fragDoctor.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                if (bundle != null) {
                    int count = bundle.getInt("count");
                    if (count > 0) {
                        doctorCircle.setVisibility(View.VISIBLE);
                    } else {
                        doctorCircle.setVisibility(View.GONE);
                    }
                }
            }
        });

        fragMy.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                if (bundle != null) {
                    int count = bundle.getInt("count");
                    if (count > 0) {
                        myCircle.setVisibility(View.VISIBLE);
                    } else {
                        myCircle.setVisibility(View.GONE);
                    }
                }
            }
        });
        changeFragment(R.id.main_content, fragVisit);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @OnClick({R.id.bottombar_home, R.id.bottombar_doctor, R.id.bottombar_health, R.id.bottombar_my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bottombar_home:
                visitIcon.setImageResource(R.drawable.ic_consult_selected);
                doctorIcon.setImageResource(R.drawable.ic_doctor);
                healthIcon.setImageResource(R.drawable.ic_health);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragVisit);
                break;
            case R.id.bottombar_doctor:
                visitIcon.setImageResource(R.drawable.ic_consult);
                doctorIcon.setImageResource(R.drawable.ic_doctor_selected);
                healthIcon.setImageResource(R.drawable.ic_health);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragDoctor);
                break;
            case R.id.bottombar_health:
                visitIcon.setImageResource(R.drawable.ic_consult);
                doctorIcon.setImageResource(R.drawable.ic_doctor);
                healthIcon.setImageResource(R.drawable.ic_health_selected);
                myIcon.setImageResource(R.drawable.ic_my);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                changeFragment(R.id.main_content, fragHealth);
                break;
            case R.id.bottombar_my:
                visitIcon.setImageResource(R.drawable.ic_consult);
                doctorIcon.setImageResource(R.drawable.ic_doctor);
                healthIcon.setImageResource(R.drawable.ic_health);
                myIcon.setImageResource(R.drawable.ic_my_selected);
                visitTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                doctorTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                healthTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.txt_black2));
                myTxt.setTextColor(ContextCompat.getColor(this.getBaseContext(), R.color.main_text_02));
                changeFragment(R.id.main_content, fragMy);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (!isActive) {
            //app 从后台唤醒，进入前台
            isActive = true;
        }
        appContext = (AppContext) getApplicationContext();
        custom = appContext.custom;
        if (custom != null && !custom.isEmpty()) {
            turnActivity();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!isAppOnForeground()) {
            //app 进入后台
            //全局变量
            isActive = false;
            // 记录当前已经进入后台
            Log.e("jx", "base Stop()");
        }
    }

    /**
     * 程序是否在前台运行
     *
     * @return
     */
    public boolean isAppOnForeground() {
        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        return false;
    }

    private void turnActivity() {
        PushData pushData = JSON.parseObject(custom, PushData.class);
        if (pushData.getMsgType().equals(ActValue.Patient_Question.getNum() + "")) {
            Intent intent = new Intent(aty.getApplicationContext(), Act_Question.class);
            startActivity(intent);
        }
        appContext.custom = "";
    }
}
